#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Practical introduction to Qt in Python
# Copyright (C) 2011-2012  Gonzalo Exequiel Pedone
#
# This Program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This Program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with This Program.  If not, see <http://www.gnu.org/licenses/>.
#
# Email   : hipersayan.x AT gmail.com
# Web-Site: http://hipersayanx.blogger.com/

import sys

from PyQt4 import QtCore, QtGui, uic


class AddURL(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)

        uic.loadUi('../share/ui/addurl.ui', self)

        # We store all URLs added by the user in a list.
        self.urls = []

        # When the user click the "Ok" button..
        self.bbxAcceptCancel.accepted.connect(self.on_bbxAcceptCancel_accepted)

        # When the user click the "Cancel" button..
        self.bbxAcceptCancel.rejected.connect(self.on_bbxAcceptCancel_rejected)

    @QtCore.pyqtSlot()
    def on_bbxAcceptCancel_accepted(self):
        # Read the text from QPlainTextEdit.
        # Each line is an URL.
        # I don't add url checking for no complicating the code.
        self.urls = str(self.txtURLList.toPlainText()).split()

        # Sort the URL list.
        self.urls.sort()

        # Close the dialog and return 0, this means that the user accepted the
        # action.
        self.done(0)

    @QtCore.pyqtSlot()
    def on_bbxAcceptCancel_rejected(self):
        # Close the dialog and return 1, this means that the user cancelled the
        # action.
        self.done(1)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    translator = QtCore.QTranslator(app)
    locale = QtCore.QLocale.system().name()
    translator.load(locale + '.qm', '../share/ts')
    app.installTranslator(translator)

    add_url = AddURL()
    add_url.show()
    app.exec_()
